﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WXWeb
{
    public partial class BootStrap : System.Web.UI.MasterPage
    {
        Access ac = new Access();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["UserInfo"] != null)
                {
                    Model.UserInfo user = Session["UserInfo"] as Model.UserInfo;
                    lblUserName.Text = user.Name;

                    string lblMenu = @"<li id='search'>
                    <a href='javascript:;'><i class='fa fa-search opacity-control'></i></a>
                     <form>
                        <input type='text' class='search-query' placeholder='Search...'>
                        <button type='submit'><i class='fa fa-search'></i></button>
                    </form>
                </li>
                <li class='divider'></li>
                <li><a href='/index.aspx'><i class='fa fa-home'></i> <span>微信总览</span></a></li>";
                    DataTable dt = ac.GetNewMenu("");
                    List<string> list = new List<string>();
                    DataRow[] rows = dt.Select("parentid=1");
                    DataRow[] rows3 = null;
                    int i = 0;
                    foreach (DataRow dr in rows)
                    {
                        if (dr["haveC"].ToString() == "haveC")
                        {
                            i++;
                            lblMenu += string.Format("<li><a href=\"javascript:;\"><i class=\"{1}\"></i> <span>{0}</span> </a><ul class=\"acc-menu\">",  dr["MenuName"].ToString(),dr["menuicon"].ToString());

                                    DataRow[] rows2 = dt.Select("parentid=" + dr["ID"].ToString());
                                   // lblMenu2.Text += " <div><span>" + dr["MenuName"].ToString() + "</span><div style='width: 150px;'>";
                                    foreach (DataRow d in rows2)
                                    {
                                        lblMenu += string.Format(" <li><a href=\"{0}\"><span>{1}</span></a></li>",GetSitePath()+d["MenuUrl"].ToString(), d["MenuName"].ToString() );
                                    }
                                    lblMenu += " </ul> </li>";

                            
                        }
                        else
                        {
                            lblMenu += string.Format("<li><a href=\"{0}\" {3}><i class=\"{2}\"></i> <span>{1}</span></a></li>", GetSitePath()+dr["MenuUrl"].ToString(), dr["MenuName"].ToString(), dr["menuicon"].ToString(), dr["MenuName"].ToString() == "菜单列表" ? "target='_blank'" : "");
                        }
                    }

                    sidebar.InnerHtml = lblMenu;
                   // div1.InnerHtml = lblMenu;
                }
            }
        }

        /// <summary>
        /// 获取站点根路径
        /// </summary>
        /// <returns></returns>
        public string GetSitePath()
        {
            string root = Page.Request.ApplicationPath;
            if (root == "/") return root;
            else return root + "/";
        }
    }
}